﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace NetChatInterface
{
    public partial class FileBrowseTransmission : Form
    {
        //vari
        TcpClient tc;
        public IPEndPoint ip;
        private Stack<string> filePath = new Stack<string>();
        private string CurrentPath;
        byte[] ReceiveBuffer = new byte[1024];
        //
        public FileBrowseTransmission()
        {
            InitializeComponent();
        }
        private Stack<string> GetDirectories(string CurPath)
        {
            Stack<string> filepath = new Stack<string>();
            DirectoryInfo dirinfo = new DirectoryInfo(CurPath);
            try
            {
                foreach (DirectoryInfo dirin in dirinfo.GetDirectories())
                {
                    string tmpstr = dirin.Attributes.ToString().Replace("Hidden", "");
                    if (dirin.Attributes.ToString().Length == tmpstr.Length)
                        filepath.Push(dirin.FullName);
                }
                foreach (FileInfo filein in dirinfo.GetFiles())
                {
                    string tmpstr = filein.Attributes.ToString().Replace("Hidden", "");
                    if (filein.Attributes.ToString().Length == tmpstr.Length)
                        filepath.Push(filein.FullName);
                }
                return filepath;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex + ",文件查看失败");
                return null;
            }
        }
        
        private void FileBrowseTransmission_Load(object sender, EventArgs e)
        {
            tc = new TcpClient(ip);
            this.SetDesktopLocation(400, 400);
            DOWNLOAD_LIST.Columns.Add("文件名");
  
            //GetDrivers();
        }

        private void BTN_CLOSE_Click(object sender, EventArgs e)
        {

            this.Hide();
            tc.Close();
        }

        private void BTN_REFRESH_Click(object sender, EventArgs e)
        {
            
        }

        private void DOWNLOAD_LIST_Click(object sender, EventArgs e)
        {
            
        }

        private void DOWNLOAD_LIST_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (DOWNLOAD_LIST.SelectedItems.Count > 0)
            {
                CurrentPath = DOWNLOAD_LIST.SelectedItems[0].Text.ToString();
                filePath.Push(CurrentPath);
                //GetDirectoriesAndFiles(CurrentPath);
               
             }   
        }

        private void BTN_LASTLEVEL_Click(object sender, EventArgs e)
        {
            if (filePath.Count > 1)
            {
                filePath.Pop();
                CurrentPath = filePath.Peek();
                ///GetDirectoriesAndFiles(CurrentPath);
             
            }
            else if (filePath.Count == 1)
            {
                //GetDrivers();//列出驱动器
            }
            else MessageBox.Show("已经到达最上级");
            
        }

        private void BTN_DOWNLOAD_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowDialog();
            //首先获取file的名字
            try
            {
            NetworkStream ns = tc.GetStream();
            ns.Read(ReceiveBuffer, 0, ReceiveBuffer.Length);
            string filename = Encoding.Unicode.GetString(ReceiveBuffer);
            //生成一个跟传来文件名字一样的文件
            FileStream fs = new FileStream(fbd.SelectedPath+filename, FileMode.Create);
             //循环把文件读取完
                bool isRead = true;
                while (isRead)
                {
                    int count = ns.Read(this.ReceiveBuffer, 0, this.ReceiveBuffer.Length);
                    if (count < ReceiveBuffer.Length)
                        isRead = false;
                    fs.Write(this.ReceiveBuffer, 0, count);
                    //TEXT_FILE.Text = "文件传送完毕";
                }

                fs.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "frmReceive::Receive");
            }
            
        }
    }
}


///////////////////////////////////////////////////////////
//old version
/*
   private void GetDirectoriesAndFiles(string CurPath)
        {
            DirectoryInfo dirinfo = new DirectoryInfo(CurPath);
            DOWNLOAD_LIST.Items.Clear();
            try
            {
                foreach (DirectoryInfo dirin in dirinfo.GetDirectories())
                {
                    string tmpstr = dirin.Attributes.ToString().Replace("Hidden", "");
                    if (dirin.Attributes.ToString().Length == tmpstr.Length)
                        DOWNLOAD_LIST.Items.Add(dirin.FullName);
                }
                foreach (FileInfo filein in dirinfo.GetFiles())
                {
                    string tmpstr = filein.Attributes.ToString().Replace("Hidden", "");
                    if (filein.Attributes.ToString().Length == tmpstr.Length)
                        DOWNLOAD_LIST.Items.Add(filein.FullName);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex + ",文件查看失败");
            }
        }
*/

/*
        private void GetDrivers()
        {
            string[] str = Directory.GetLogicalDrives();
            DOWNLOAD_LIST.Items.Clear();
            foreach (string s in str)
            {
                DOWNLOAD_LIST.Items.Add(s);
            }
        }
*/